<?php

namespace app\payment\model;

use app\payment\model\PaymentCash;
use think\Model;

/**
 * 提现支付宝模型
 */
class WithdrawalAlipay extends Model {

    // 设置当前模型对应的完整数据表名称
    protected $name = 'payment_withdrawal_alipay';
    // 主键
    protected $pk = 'order_id';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 提现次数
     * @param  string $merchant_id   商户号
     * @param  string $status        提现状态
     * @param  string $day           日期
     * @return mixed
     */
    public static function withdrawalCount($merchant_id = '', $status = '', $day = '') {

//        $merchant_ids = $merchant_id ? ['merchant_id' => $merchant_id] : [];
//        $statuss = $status !== '' ? ['status' => $status] : [];
//        $days = $day ? ['create_time' => ['between time', ["{$day} 00:00:00", "{$day} 23:59:59"]]] : [];
//        $map = array_merge($merchant_ids, $statuss, $days);

        $map = [];
        if ($merchant_id) {
            $map[] = ['merchant_id', '=', $merchant_id];
        }
        if ($status) {
            $map[] = ['status', '=', $status];
        }
        if ($day) {
            $map[] = ['create_time', 'between time', ["{$day} 00:00:00", "{$day} 23:59:59"]];
        }
        $counts = self::where($map)->count();
        return $counts;
    }

    /**
     * 提现金额
     * @param  string $merchant_id   商户号
     * @param  string $status        提现状态
     * @param  string $day           日期
     * @return mixed
     */
    public static function withdrawalCash($merchant_id = '', $status = '', $day = '') {

//        $merchant_ids = $merchant_id ? ['merchant_id' => $merchant_id] : [];
//        $statuss = $status !== '' ? ['status' => $status] : [];
//        $days = $day ? ['create_time' => ['between time', ["{$day} 00:00:00", "{$day} 23:59:59"]]] : [];
//        $map = array_merge($merchant_ids, $statuss, $days);

        $map = [];
        if ($merchant_id) {
            $map[] = ['merchant_id', '=', $merchant_id];
        }
        if ($status) {
            $map[] = ['status', '=', $status];
        }
        if ($day) {
            $map[] = ['create_time', 'between time', ["{$day} 00:00:00", "{$day} 23:59:59"]];
        }
        $cash = self::where($map)->sum('cash');
        return $cash;
    }

    /**
     * 可用金额
     * @param  string $merchant_id   商户号
     * @param  string $status        状态
     * @return mixed
     */
    public static function surplusCash($merchant_id = '', $status = 1) {

//        $merchant_ids = $merchant_id ? ['merchant_id' => $merchant_id] : [];
//        $statuss = $status !== '' ? ['status' => $status] : [];
//        $map = array_merge($merchant_ids, $statuss);

        $map = [];
        if ($merchant_id) {
            $map[] = ['merchant_id', '=', $merchant_id];
        }
        if ($status) {
            $map[] = ['status', '=', $status];
        }
        $cash = PaymentCash::where($map)->value('cash');
        return $cash;
    }

}
